package com.example.metadatatest.CustomizeExecutor;


import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.context.event.SimpleApplicationEventMulticaster;
import org.springframework.context.support.AbstractApplicationContext;

import java.util.concurrent.*;

/**
 * MyExecutor
 *
 * @blame 吳經綸
 */
@Configuration
public class MyExecutor {
    @Bean(name = "MyCustomExecutor")
    public Executor MyCustomExecutor()
        {
            ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("thread-call-runner-%d").setThreadFactory(Executors.defaultThreadFactory()).build();
            return new ThreadPoolExecutor(2,5,
                1L, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(3),
                    namedThreadFactory,
                new ThreadPoolExecutor.AbortPolicy());
    }

    @Bean(AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME)
    public ApplicationEventMulticaster myCustomizeApplicationEventMulticaster(@Qualifier("MyCustomExecutor") Executor executor)
    {
        SimpleApplicationEventMulticaster simpleApplicationEventMulticaster = new SimpleApplicationEventMulticaster();
        simpleApplicationEventMulticaster.setTaskExecutor(executor);
        return simpleApplicationEventMulticaster;
    }


}
